package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.eotms.dto.base.UserProfileDto;
import cn.buk.tms.eotms.entity.AmountHistory;
import cn.buk.tms.eotms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yfdai
 */
@RestController
public class UserController extends BaseController {

  @Autowired
  public UserController(UserService userService) {
    this.userService = userService;
  }

  private final UserService userService;

  @RequestMapping("/recharge")
  public JsonResult recharge(HttpServletRequest request,
                             @RequestParam("amount") float amount) {
    final int userId = getUserId(request);

    return userService.recharge(userId, amount);
  }

  @GetMapping({"/profile", "/my/profile"})
  public UserProfileDto getProfile(HttpServletRequest request) {
    return userService.getUserProfile(getUserId(request));
  }

  /**
   * 企业客户的交易记录
   */
  @RequestMapping("/my/trans")
  public CommonDto<AmountHistory> searchTrans(HttpServletRequest request,
                                              @ModelAttribute("sc") CommonSearchCriteria sc) {
    List<AmountHistory> histories = userService.searchTransHistory(getUserId(request), sc);

    CommonDto<AmountHistory> commonDto = new CommonDto<>();
    commonDto.setDataList(histories);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }
}
